/**
 * @author comahead
 */
bugs_trac = {
	p_cnt : "",
	p_name : "",
	p_amt : "",
	p_tracking_code : "b"+"_t_"+"u_",
	
	escape : function(str){
		var result = escape(str);
		result = result.split("+").join("%2B");
		result = result.split("/").join("%2F");
		result = result.split("&").join("%26");
		result = result.split("?").join("%3F");
		result = result.split(":").join("%3A");
		result = result.split("#").join("%23");
		return result;
	},
	
	cookie : {
		unescape : function(str){
			var result = unescape(str);
			result = result.split("%2B").join("+");
			result = result.split("%2F").join("/");
			result = result.split("%26").join("&");
			result = result.split("%3F").join("?");
			result = result.split("%3A").join(":");
			result = result.split("%23").join("#");
			return result;
		},
		
		set : function(name, value) {		 	
		 	document.cookie = name + "=" +value + "; path = /; domain=" + document.domain + ";";
		},
		get : function(name) {
			var dcookie = document.cookie;
			var cname = name + "=";
			var clen = dcookie.length;
			var cbegin = 0;
			while (cbegin < clen) {
				var vbegin = cbegin + cname.length;
					if (dcookie.substring(cbegin, vbegin) == cname) {
						var vend = dcookie.indexOf (";", vbegin);
						if (vend == -1) vend = clen;
					return this.unescape(dcookie.substring(vbegin, vend));
				}
				cbegin = dcookie.indexOf(" ", cbegin) + 1;
				if (cbegin == 0) break;
			}
			return "";
		}
	},
	
	trac_log : function(code){
		//트래킹내용 DB저장
		//트래킹 코드가 있는 경우  분기
		var target_code = ( arguments.length == 3) ? arguments[1]:null;
		//추가 코드
		var buy_tp = ( arguments.length == 3) ? arguments[2]:null;
		
		var sUrl = this.cookie.get(this.p_tracking_code+code);
		var tUrl = self.location.href;
		var tQuery = self.location.search;
		var tLength = 0;
		if(sUrl==null || sUrl=="" || sUrl=="undefined") return null;
		
		var strDomain = null;
		var strMenu = null;
		var strSubMenu = null;
		
		//쿠키에서 url정보 읽어온다
		var tmpArray = sUrl.split("/");
		
		strDomain = tmpArray[0].split(".")[0];
		if(tmpArray.length >= 2){
			strMenu = tmpArray[1];
		}
		if(tmpArray.length >= 3){
			strSubMenu = tmpArray[2];
		}
		
//		alert("domain:"+strDomain+"\nstrMenu:"+strMenu+"\nstrSubMenu:"+strSubMenu+"\nlastLoc:"+target_code+"\nbuy_tp:"+buy_tp);
		//g_urlMember
		try{
			var params = { domain : strDomain, menu : strMenu, subMenu : strSubMenu, lastLoc : target_code, buyTp : buy_tp};
		
			$.post(
				g_urlMember+"/trac/"+code,
				params
			);
		}catch(e){
			var imgUrl="<img src='"+g_urlMember+"/trac/"+code
				+"?domain="+strDomain+"&menu="+strMenu+"&subMenu="+strSubMenu+"&lastLoc="+target_code+"&buyTp="+buy_tp
				+"' width=0 height=0/>";
			document.write(imgUrl);
		}		
	},
	make_trac : function(code){
		//트래킹정보 쿠키저장
		if(code=="" || code=="undefined" || code == null){ return null; }
		var referrer = self.document.referrer;
		var href = self.location.href;
		var sUrl = "";
		var tLength = 0;
		var selfUrl = "";
		
		tLength = href.indexOf("?");
		if(tLength > 0){
			selfUrl = href.substring(0, tLength);
		}else{
			selfUrl = href;
		}
		
		if(code == "1"){
			sUrl = selfUrl;
		}else{
			if(referrer == ""){	return null; }
			
			tLength = referrer.indexOf("?");
			if(tLength > 0){
				sUrl = referrer.substring(0, tLength);
			}else{
				sUrl = referrer;
			}
			if(sUrl == selfUrl){ return null;}
		}
		
		sUrl = sUrl.substring(sUrl.indexOf("//")+2, sUrl.length);
		this.cookie.set(this.p_tracking_code+code, this.escape(sUrl));
		
		if(code == "2"){
			//단순 RAW인경우는 바로 등록한다.
			this.trac_log(code);
		}
		return true;
	}
}